<?php
require_once ('CommonUtils.php');

class Keywords extends BaseModel {

	public static $_instances = array ();

	protected $_primary = "keywordsID";

	protected $_name = "keywords";

	protected $arrobj = null;
	
	protected $simpleArrobj = null;

	public function __construct($type) {
		parent::__construct ( null );
		$this->_name = ObjectType::$KEYWD_TYPES [$type];
	}

	public function abstractLoad($id) {}

	public function createDefaultData() {}

	public static function getInstance($type) {
		foreach ( ObjectType::$DEF_TYPES as $key => $x ) {
			if ($type == $key) {
				if (! isset ( self::$_instances [$key] )) {
					$c = __CLASS__;
					$tmp = new $c ( $key );
					self::$_instances [$key] = $tmp;
				}
				require_once 'ObjectCache.php';
				$objCache = new ObjectCache ( self::$_instances [$key], CacheConfig::$staticCacheLifeTime);
				return $objCache->getCache();
			}
		}
		return null;
	}
	
	public function getSimpleKeywords(){
		if($this->simpleArrobj !== null) {
			return $this->simpleArrobj;
		}
		$simpleArrobj = array();
		$statement = $this->select ();
		$result = $this->fetchAll ( $statement );
		$this->simpleArrobj = $result;
		return $this->simpleArrobj;
	}
	
	public function getKeywordForSearch(){
		if($this->arrobjForSearch !== null){
			return $this->arrobjForSearch;
		}
		$stmt = "SELECT keyword, keywordsContentVi as content FROM keywords WHERE formatter NOT LIKE 'Hidden' AND maxInstance <> 0 ORDER BY priority";
		$result = $this->_db->query ( $stmt );
		$rs = $result->fetchAll();
		$this->arrobjForSearch = $rs;
		return $rs ;
	}
	
	public function getKeywordsContent(){
		$stmt = "SELECT keyword, keywordsContentVi as content FROM keywords";
		$result = $this->_db->query ( $stmt );
		$rs = $result->fetchAll();
		return $rs ;
	}
	
	public function getKeywords() {
		if ($this->arrobj !== null) {
			return $this->arrobj;
		}
		$arrobj = array ();
		$statement = $this->select ()->order("priority ASC");
		$result = $this->fetchAll ( $statement );
		for($i = 0; $i < count ( $result ); $i ++) {
			//set parent
			$row = $result [$i];
			$kwID = $row ['keywordsID'];
			$kwPID = $row ['keywordsPID'];
			//set children
			$arrobj [$kwID] ['p_oid'] = $kwPID;
			if (! isset ( $arrobj [$kwPID] )) {
				$arrobj [$kwPID] = array ();
			}
			if (! isset ( $arrobj [$kwPID] ['child'] )) {
				$arrobj [$kwPID] ['child'] = array ();
			}
			$arrobj [$kwPID] ['child'] [] = $kwID;
			$arrobj [$kwID] ['keyword'] = $row ['keyword'];
			if (! isset ( $arrobj [$kwID] ['child'] )) {
				$arrobj [$kwID] ['child'] = array ();
			}
			$arrobj [$kwID] ['parent'] = $row ['keywordsPID'];
			$arrobj [$kwID] ['keywordsContentEn'] = $row ['keywordsContentEn'];
			$arrobj [$kwID] ['keywordsContentVi'] = $row ['keywordsContentVi'];
			$arrobj [$kwID] ['maxInstance'] = $row ['maxInstance'];
			$arrobj [$kwID] ['formatter'] = $row ['formatter'];
			$arrobj [$kwID] ['viewFormatter'] = $row ['viewFormatter'];
			$arrobj [$row ['keyword']] = $arrobj [$kwID];
			$arrobj [$row ['keyword']] ['oid'] = $kwID;
		}
		$this->arrobj = $arrobj;
		return $arrobj;
	}
	
	public function getDisplayKeyWord($kw,$lang){
		$keyword = CommonUtils::getKeywordLang();
		$arrkw = $this->getKeywords();
		if (!isset($arrkw[$kw]))	{
			return "Unknown keyword ($kw)";
		}
		return $arrkw[$kw][$keyword];
	}
}
?>
